package top.laoliu.interceptor;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import top.laoliu.pojo.Company;
import top.laoliu.pojo.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: acer
 * @Date: 2020/10/20 16:23
 * @Description:
 */
public class CompanyLoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        request.setCharacterEncoding("UTF-8");
        String url = request.getServletPath();
        System.out.println("post URL：" + url);
        if (!url.equals("")) {
            //判断是否已登录
            Company loginCompany = (Company) request.getSession().getAttribute("company");
            if (loginCompany == null) {
                //无session则是无登录状态
                System.out.println(">>>未登录，請重新登录<<<");
                response.sendRedirect("/user/toLoginPage");
                return false;
            }
        }
        return true;
    }
}
